package com.knife.blog.web.action;

import org.apache.log4j.Logger;

import com.knife.blog.domain.BlogNewsDoc;
import com.knife.blog.domain.BlogReview;
import com.knife.blog.domain.Photo;
import com.knife.blog.logic.BlogNewsDocService;
import com.knife.blog.logic.BlogReviewService;
import com.knife.blog.logic.impl.BlogCoreServiceImpl;
import com.knife.blog.logic.impl.BlogReviewServiceImpl;
import com.knife.blog.logic.impl.PhotoServiceImpl;
import com.knife.util.CommUtil;
import com.knife.util.TagUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;

public class BlogReviewAction extends BaseCrudAction {
    private static final Logger logger = (Logger) Logger.getLogger(BlogReviewAction.class.getName());
    private BlogReviewService blogReviewService = BlogReviewServiceImpl.getInstance();
    private BlogNewsDocService blogDocService = BlogCoreServiceImpl.getInstance();

    public IPageList doQuery(WebForm form, int currentPage, int pageSize) {
        return this.blogReviewService.queryUserBlogReviews(getCurrentUser(form).getUserName(), currentPage, pageSize);
    }

    public Page doUpdate(WebForm form, Module module) {
        BlogReview obj = (BlogReview) form2Obj(form);
        boolean ret = this.blogReviewService.updateBlogReview(obj);
        form.addResult("msg", (ret) ? "修改成功!" : "修改失败!");
        return doList(form, module);
    }

    @Override
    public Page doDel(WebForm form, Module module, IActiveUser user) {
        String cid = (String) form.get("cid");
        boolean ret = this.blogReviewService.delBlogReview(cid);
        form.addResult("msg", (ret) ? "删除成功" : "删除失败!");
        try {
            int currentPage = CommUtil.null2Int(form.get("currentPage"));
            int PageSize = CommUtil.null2Int(form.get("PageSize"));
            if (currentPage < 1) {
                currentPage = 1;
            }
            if (PageSize < 1) {
                PageSize = 15;
            }
            IPageList pList = doQuery(form, currentPage, PageSize);
            form.addResult("list", pList.getResult());
            form.addResult("pages", new Integer(pList.getPages()));
            form.addResult("rows", new Integer(pList.getRowCount()));
            form.addResult("page", new Integer(pList.getCurrentPage()));
            form.addResult("gotoPageHTML", CommUtil.showPageHtml(pList.getCurrentPage(), pList.getPages()));
        } catch (Exception e) {
            System.out.println("获取评论列表信息时出现了异常,请检查系统日志");
            logger.error(e);
        }
        return module.findPage("list");
    }

    public Page doEdit(WebForm form, Module module) {
        String cid = (String) form.get("cid");
        //System.out.println("cid====" + cid);
        BlogReview blogreview = this.blogReviewService.getBlogReview(cid);
        form.addPo(blogreview);
        return module.findPage("edit");
    }

    public Page doCheck(WebForm form, Module module) {
	String ids[]=((String)form.get("mulitCid")).split(",");
	boolean ret=false;
	for(int i=0;i<ids.length;i++){
            ret=blogReviewService.check(ids[i]);
	}
	form.addResult("msg",ret?"审核成功!":"审核失败!");
	return this.doUlist(form,module);
    }

    public Page doList(WebForm form, Module module) {
        int currentPage = CommUtil.null2Int(form.get("page"));
        int PageSize = CommUtil.null2Int(form.get("PageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (PageSize < 1) {
            PageSize = 15;
        }
        try {
            IPageList pList = this.blogReviewService.queryUserBlogReviewsAll("inputtime", currentPage, PageSize);
            form.addResult("list", pList.getResult());
            form.addResult("pages", new Integer(pList.getPages()));
            form.addResult("rows", new Integer(pList.getRowCount()));
            form.addResult("page", new Integer(pList.getCurrentPage()));
            form.addResult("gotoPageHTML", CommUtil.showPageHtml(pList.getCurrentPage(), pList.getPages()));
        } catch (Exception e) {
            System.out.println("获取评论列表信息时出现了异常,请检查系统日志");
            logger.error(e);
        }
        return module.findPage("list");
    }

    public Page doUlist(WebForm form, Module module) {
        int currentPage = CommUtil.null2Int(form.get("page"));
        int PageSize = CommUtil.null2Int(form.get("PageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (PageSize < 1) {
            PageSize = 15;
        }
        try {
            IPageList pList = this.blogReviewService.queryUserBlogUnReview("inputtime", currentPage, PageSize);
            form.addResult("list", pList.getResult());
            form.addResult("pages", new Integer(pList.getPages()));
            form.addResult("rows", new Integer(pList.getRowCount()));
            form.addResult("page", new Integer(pList.getCurrentPage()));
            //form.addResult("gotoPageHTML", CommUtil.showPageHtml(pList.getCurrentPage(), pList.getPages()));
            form.addResult("gotoPageHTML", TagUtil.showPageHtml(pList.getCurrentPage(), pList.getPages()));
        } catch (Exception e) {
            System.out.println("获取评论列表信息时出现了异常,请检查系统日志");
            logger.error(e);
        }
        return module.findPage("ulist");
    }

    public Page doPost(WebForm form, Module module) {
        BlogReview obj = (BlogReview) form2Obj(form);
        String type = (String) form.get("type");
        boolean ret = false;
        if (type.equals("doc")) {
            ret = this.blogReviewService.addBlogReview(obj);
        }
        if (type.equals("photo")) {
            ret = this.blogReviewService.addPhotoReview(obj);
        }
        form.addResult("msg", (ret) ? "评论成功,审核中!" : "评论失败，可能是权限不够或者输入的内容不合法!");
        String url = "/blogReview.do?parameter=show&type=" + type + "&pid=" + obj.getParentid();
        form.addResult("url", url);
        form.addResult("time", "1");
        return new Page("postOk", url, "html");
    }

    public Object form2Obj(WebForm form) {
        String cid = CommUtil.null2String(form.get("cid"));
        BlogReview obj = this.blogReviewService.getBlogReview(cid);
        if (obj == null) {
            obj = new BlogReview();
        }
        form.toPo(obj);
        return obj;
    }

    public Page doShow(WebForm form, Module module) {
        String cid = CommUtil.null2String(form.get("pid"));
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize < 1) {
            pageSize = 15;
        }
        IPageList pageList = this.blogReviewService.queryBlogReview(cid, currentPage, pageSize);
        CommUtil.saveIPageList2WebForm(pageList, form);
        if (pageList != null) {
            form.addResult("pageSize", new Integer(pageSize));
        }

        String type = CommUtil.null2String(form.get("type"));
        if (type.equals("photo")) {
            Photo obj = PhotoServiceImpl.getInstance().getPhoto(cid);
            form.addResult("obj", obj);
            this.blog = this.userService.getUserBlog(obj.getInputuser());
            return new Page("review" + this.blog.getUsername(), "/user/photoreviewShow.htm", "template");
        }
        BlogNewsDoc obj = this.blogDocService.getBlogNewsDoc(cid);
        form.addResult("obj", obj);
        this.blog = this.userService.getUserBlog(obj.getInputuser());
        return new Page("review" + this.blog.getUsername(), "/user/reviewShow.htm", "template");
    }

    public Page doCountUnCheck(WebForm form, Module module){
        int ret=0;
        ret = blogReviewService.countUnCheckMessage(blog.getUsername());
        form.addResult("result", ret);
        return module.findPage("ajaxResult");
    }
}
